package com.packet.controller;

import com.packet.pojo.Group;
import com.packet.pojo.Inform;
import com.packet.pojo.InformY;
import com.packet.pojo.Member;
import com.packet.service.GroupService;
import com.packet.service.InformService;
import com.packet.service.MemberService;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName:InformController
 * @Author: mu
 * @Date:2024-06-25 19:26
 * @Describe:
 */
@Controller
public class InformController {
    @Autowired
    private InformService informService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private GroupService groupService;
    @RequestMapping("/staff/getAllInform")
    public String getAllInform(Model model, HttpSession session){
        Integer sid = (Integer)session.getAttribute("sid");
        List<Inform> allInform = informService.getAllInform(sid);
        System.out.println(allInform);
        model.addAttribute("allInform",allInform);

        return "/staff/Trip/getAllInform";
    }

    @RequestMapping("/staff/addInform")
    public String addInform(Model model,HttpSession session){
        Integer sid = (Integer)session.getAttribute("sid");
        List<Group> myGroup = groupService.getMyGroup(sid);
        model.addAttribute("myGroup",myGroup);
        return "/staff/Trip/addInform";
    }

    @RequestMapping("/staff/addInformToAll")
    public String addInformToAll(Model model,HttpSession session,String info,int Gid){
        List<Member> allMember = memberService.getAllMember(Gid);
        Integer sid = (Integer)session.getAttribute("sid");
        List<InformY> allInformY = new ArrayList<>();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for (Member member:allMember) {
            InformY informY = InformY.builder().sId(sid)
                    .info(info)
                    .ItosId(member.getSId())
                    .ItoTime(formatter.format(date))
                    .build();
            System.out.println(sid);
            System.out.println(info);
            System.out.println(member.getSId());
            System.out.println(formatter.format(date));
            allInformY.add(informY);
        }
        System.out.println(allInformY);
        informService.addInformToAll(allInformY);
        return "redirect:/staff/getMyGroup";
    }


}
